package com.itheima.aop;

import com.itheima.pojo.Result;
import com.itheima.util.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

// 控制数据统计报告的切面
@Component
// @Aspect
public class ReportControlAspect {
    @Autowired
    private HttpServletRequest request;

    @Around("execution(* com.itheima.controller.ReportController.*(..))")
    public Object controlReport(ProceedingJoinPoint pjp) throws Throwable {
        // 1.拿到登录的员工id
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        Integer empId = (Integer) claims.get("id");

        // 3.如果员工id <= 10, 执行方法
        if (empId <= 10) {
            Object ret = pjp.proceed();
            return ret;
        }

        // 2.如果员工id > 10, 不执行方法
        return Result.error("你是普通员工, 没有权限!");
    }
}
